﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports ISNet.WebUI.WebGrid
Imports System.Data
Imports ISNet.WebUI.WebTreeView
Imports log4net

Partial Class P01
    Inherits System.Web.UI.Page

    Dim cmmtcode As String = ""
    Public LifeConfig1 As String
    Public LifeConfig2 As String
    Public LifeConfig3 As String
    Dim lockhash As New Hashtable

    Private Shared logger As ILog = LogManager.GetLogger("P01")

    Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
        WebCombo3.EncodeOutput = True
    End Sub
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        cmmtcode = Session("CMMT_OT_CODE")
        PlanYearsTxt.Text = Session("PlanYear")
        VersionTxt.Text = Session("Versions")
        If Session("FCTR") Is Nothing Then
            Response.Redirect("Selector.aspx?type=41")
        End If
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If

        GenerateValues()

        Session("multiply") = 0
        If IsPostBack = False Then
            Session.Remove("Search")
            Session.Remove("search_fctr")
            Session("multiply") = 1


            Label1.Text = "ระบบจัดทำงบประมาณ >> จัดทำงบประมาณ >> กำหนดเป้าหมายเงินฝาก "
            'If Session("Versions") = 1 Then
            '    div1.Visible = False
            '    div2.Visible = False
            '    SearhBtn.Visible = False
            '    WebTreeView1.Visible = False
            '    WebTreeView2.Visible = False
            '    WebTreeView3.Visible = False
            'End If
            Dim adapter As New SplitBudgetDSTableAdapters.ProductsTableAdapter

            Dim data As New SplitBudgetDS.ProductsDataTable
            'data = adapter.GetDataByPlanYear(Val(Session("PlanYear")))

            data = adapter.GetDataByP01(PlanYearsTxt.Text)
            ms8.DataValueField = "ProductCode"
            ms8.DataTextField = "Description"
            ms8.DataSource = data
            ms8.DataBind()

        End If
        'If WebGrid1.IsFlyPostBack = False Then
        '    PlotNode()

        'End If

        Dim prefil2 As String = ""
        prefil2 = Session("prefilter")
        Dim re_adap As New FormLockDSTableAdapters.FundCenterReleasesTableAdapter
        Dim rsql As String = ""
        rsql = " SELECT FundCenterReleases.FundCenter_FundCenterCode FROM FundCenterReleases "
        rsql += " WHERE PlanYear = " & Val(PlanYearsTxt.Text) & " "
        rsql += " AND Version = '" & VersionTxt.Text & "' "
        rsql += " AND FundCenter_FundCenterCode IN (" & prefil2 & ")" : rsql += " AND ReleaseFlag = 'Y' "
        'Response.Write(rsql)
        'Exit Sub
        Dim re_dt As New FormLockDS.FundCenterReleasesDataTable
        re_adap.GetDataByFilter(re_dt, rsql)
        For Each ritem As FormLockDS.FundCenterReleasesRow In re_dt
            lockhash.Remove(ritem.FundCenter_FundCenterCode) : lockhash.Add(ritem.FundCenter_FundCenterCode, True)
        Next
    End Sub

    Protected Sub WebGrid1_CustomAggregate(sender As Object, e As CustomAggregateArgs)
        Exit Sub
        Dim adapter As New P01_DSTableAdapters.SumAct2TableAdapter
        Dim datatable As New P01_DS.SumAct2DataTable
        Dim act1, act2, forlast, forecur As Decimal

        Dim prefil As String = ""
        prefil = Session("prefilter")
        'If Session("Versions") > 1 Then
        If IsPostBack = True And Session("Search") = "true" Then
            Dim sql As String
            sql = "SELECT ISNULL(SUM(ActualLastYear1),0) AS sumact1 , ISNULL(SUM(ActualLastYear2),0) AS sumact2, ISNULL(SUM(ForecastLastYear1),0) AS sumforelast1, ISNULL(SUM(ForecastCurYear),0) AS sumforecur1"
            sql += " From budgetRevPlans INNER JOIN FundCenters ON BudgetRevPlans.FundCenter_FundCenterCode = FundCenters.FundCenterCode"
            sql += " INNER JOIN Products ON BudgetRevPlans.Product_ProductCode = Products.ProductCode INNER JOIN ProductItems ON BudgetRevplans.ProductItem_Id = ProductItems.Id"

            Dim fctr As String = ""
            fctr = Session("search_fctr")
            If TabContainer1.ActiveTab.ID = TabPanel1.ID Then
                sql += " WHERE 1 = 1 "
                'sql += " WHERE BudgetDetails.FundCenter_FundCenterCode BETWEEN " & Val(WebCombo1.Value) & " AND " & Val(WebCombo2.Value

                If hdf1.Value.ToString <> "" Then
                    sql += " AND BudgetRevPlans.Product_ProductCode IN ("
                    Dim Numx As Array = hdf1.Value.Split(",")
                    For i = 0 To Numx.Length - 1
                        If Val(Numx(i)) <> 0 Then
                            If i <> 0 Then
                                sql += ","
                            End If
                            sql += Numx(i).ToString
                        End If

                    Next
                    sql += ") "
                End If

                sql += " AND BudgetRevPlans.Product_ProductCode IS NOT NULL "
                sql += " And ProductItems.FormID = 'P01' "

                sql += " AND BudgetRevPlans.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND BudgetRevPlans.Version = '" & VersionTxt.Text & "'"
                If fctr <> "" Then
                    sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & fctr & ") "
                Else
                    sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & prefil & ") "
                End If
                sql += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Products.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND ProductItems.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Products.FormP01Flag = 'Y' "
                sql += " AND ProductItems.ItemNo <> 6 "
                adapter.GetSumAct2(datatable, sql)
                'Response.Write(sql)

            ElseIf TabContainer1.ActiveTab.ID = TabPanel2.ID Then
                sql += " WHERE 1 = 1 "
                'sql += " WHERE BudgetDetails.FundCenter_FundCenterCode IN (SELECT FundCenter_FundCenterCode FROM FundCenterHierarchies "
                'sql += " WHERE FundCenter_FundCenterCode = " & Val(WebCombo3.Value) & " OR ParentFundCenterHierarchy_Id = " & Val(WebCombo3.Value) & ") "
                If hdf1.Value.ToString <> "" Then
                    sql += " AND BudgetRevPlans.Product_Productcode IN ("
                    Dim Numx As Array = hdf1.Value.Split(",")
                    For i = 0 To Numx.Length - 1
                        If Val(Numx(i)) <> 0 Then
                            If i <> 0 Then
                                sql += ","
                            End If
                            sql += Numx(i).ToString
                        End If
                    Next
                    sql += ") "
                End If


                sql += " AND BudgetRevPlans.Product_ProductCode IS NOT NULL "
                sql += " And ProductItems.FormID = 'P01' "

                sql += " AND BudgetRevPlans.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND BudgetRevPlans.Version = '" & VersionTxt.Text & "'"
                If fctr <> "" Then
                    sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & fctr & ") "
                Else
                    sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & prefil & ") "
                End If
                sql += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Products.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND ProductItems.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Products.FormP01Flag = 'Y' "
                sql += " AND ProductItems.ItemNo <> 6 "
                adapter.GetSumAct2(datatable, sql)
                'Response.Write(sql)

            End If

            'Response.Write(sql)



            'datatable = adapter.GetDataByMultiple(Val(Session("PlanYear")), Val(Session("Versions")), Session("users"))
        Else
            Dim sql As String
            Dim fctr As String = ""
            fctr = Session("search_fctr")

            sql = "SELECT ISNULL(SUM(ActualLastYear1),0) AS sumact1 , ISNULL(SUM(ActualLastYear2),0) AS sumact2, ISNULL(SUM(ForecastLastYear1),0) AS sumforelast1, ISNULL(SUM(ForecastCurYear),0) AS sumforecur1"
            sql += " From budgetRevPlans INNER JOIN FundCenters ON BudgetRevPlans.FundCenter_FundCenterCode = FundCenters.FundCenterCode"
            sql += " INNER JOIN Products ON BudgetRevPlans.Product_ProductCode = Products.ProductCode INNER JOIN ProductItems ON BudgetRevplans.ProductItem_Id = ProductItems.Id"
            sql += " And ProductItems.FormID = 'P01' "

            sql += " AND BudgetRevPlans.PlanYear = " & PlanYearsTxt.Text & " "
            sql += " AND BudgetRevPlans.Version = '" & VersionTxt.Text & "'"
            If fctr <> "" Then
                sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & fctr & ") "
            Else
                sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & prefil & ") "
            End If
            sql += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "
            sql += " AND Products.PlanYear = " & PlanYearsTxt.Text & " "
            sql += " AND ProductItems.PlanYear = " & PlanYearsTxt.Text & " "
            sql += " AND Products.FormP01Flag = 'Y' "
            sql += " AND ProductItems.ItemNo <> 6 "
            adapter.GetSumAct2(datatable, sql)
            'MsgBox(sql)

        End If
        If datatable.Count > 0 Then
            act1 = datatable.Item(0).sumact1
            act2 = datatable.Item(0).sumact2
            forlast = datatable.Item(0).sumforelast1
            forecur = datatable.Item(0).sumforecur1
        End If
        'act1 = 11
        'act2 = 22
        'forlast = 33
        'forecur = 44

        If e.Column.Name = "result1" Then
            e.AggregateResult = Format(act2, "###,##0.00")
        ElseIf e.Column.Name = "tobtanx11" Then
            e.AggregateResult = Format(act1, "###,##0.00")
        ElseIf e.Column.Name = "trueyX2" Then
            e.AggregateResult = Format(forlast, "###,##0.00")
        ElseIf e.Column.Name = "trueyX23" Then
            e.AggregateResult = Format(forecur, "###,##0.00")
        End If

    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        Dim adapter As New PDataSetTableAdapters.BudgetRevPlansTableAdapter
        Dim datatable As New PDataSet.BudgetRevPlansDataTable
        Dim prefil As String = ""
        prefil = Session("prefilter")
        'If Session("Versions") > 1 Then
        If IsPostBack = True And Session("Search") = "true" Then
            Dim sql As String
            sql = " select BudgetRevPlans.id,BudgetRevPlans.FundCenter_FundCenterCode,FundCenters.Description As FCName,BudgetRevplans.Product_ProductCode AS ProductCode,Products.Description As ProductDesc,BudgetRevPlans.ProductItem_Id,ProductItems.Description As ProItemDesc"
            sql += " ,ActualLastYear2,ActualLastYear1,ForecastLastYear1,ForecastCurYear,LastYearMonth1,LastYearMonth2,LastYearMonth3,LastYearMonth4,LastYearMonth5,LastYearMonth6,LastYearMonth7,LastYearMonth8,LastYearMonth9,LastYearMonth10,LastYearMonth11,LastYearMonth12,CurYearMonth1,CurYearMonth2,CurYearMonth3,CurYearMonth4,CurYearMonth5,CurYearMonth6,CurYearMonth7,CurYearMonth8,CurYearMonth9,CurYearMonth10,CurYearMonth11,CurYearMonth12,(ActualLastYear1+ForecastLastYear1) As SumFX"
            sql += " , ProductItems.ItemNo"
            sql += " From budgetRevPlans INNER JOIN FundCenters ON BudgetRevPlans.FundCenter_FundCenterCode = FundCenters.FundCenterCode"
            sql += " INNER JOIN Products ON BudgetRevPlans.Product_ProductCode = Products.ProductCode INNER JOIN ProductItems ON BudgetRevplans.ProductItem_Id = ProductItems.Id"



            Dim fctr As String = ""
            fctr = Session("search_fctr")
            If TabContainer1.ActiveTab.ID = TabPanel1.ID Then
                sql += " WHERE 1 = 1 "
                'sql += " WHERE BudgetDetails.FundCenter_FundCenterCode BETWEEN " & Val(WebCombo1.Value) & " AND " & Val(WebCombo2.Value

                If hdf1.Value.ToString <> "" Then
                    sql += " AND BudgetRevPlans.Product_ProductCode IN ("
                    Dim Numx As Array = hdf1.Value.Split(",")
                    For i = 0 To Numx.Length - 1
                        If Val(Numx(i)) <> 0 Then
                            If i <> 0 Then
                                sql += ","
                            End If
                            sql += Numx(i).ToString
                        End If

                    Next
                    sql += ") "
                End If

                sql += " AND BudgetRevPlans.Product_ProductCode IS NOT NULL "
                sql += " And ProductItems.FormID = 'P01' "

                sql += " AND BudgetRevPlans.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND BudgetRevPlans.Version = '" & VersionTxt.Text & "'"
                If fctr <> "" Then
                    sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & fctr & ") "
                Else
                    sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & prefil & ") "
                End If
                sql += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Products.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND ProductItems.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Products.FormP01Flag = 'Y' "
                sql += " ORDER BY BudgetRevPlans.FundCenter_FundCenterCode, Products.ProductCode, ProductItems.ItemNo"

                adapter.GetDataByFilter(datatable, sql)
                'Response.Write(sql)

            ElseIf TabContainer1.ActiveTab.ID = TabPanel2.ID Then
                sql += " WHERE 1 = 1 "
                'sql += " WHERE BudgetDetails.FundCenter_FundCenterCode IN (SELECT FundCenter_FundCenterCode FROM FundCenterHierarchies "
                'sql += " WHERE FundCenter_FundCenterCode = " & Val(WebCombo3.Value) & " OR ParentFundCenterHierarchy_Id = " & Val(WebCombo3.Value) & ") "
                If hdf1.Value.ToString <> "" Then
                    sql += " AND BudgetRevPlans.Product_Productcode IN ("
                    Dim Numx As Array = hdf1.Value.Split(",")
                    For i = 0 To Numx.Length - 1
                        If Val(Numx(i)) <> 0 Then
                            If i <> 0 Then
                                sql += ","
                            End If
                            sql += Numx(i).ToString
                        End If
                    Next
                    sql += ") "
                End If


                sql += " AND BudgetRevPlans.Product_ProductCode IS NOT NULL "
                sql += " And ProductItems.FormID = 'P01' "

                sql += " AND BudgetRevPlans.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND BudgetRevPlans.Version = '" & VersionTxt.Text & "'"
                If fctr <> "" Then
                    sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & fctr & ") "
                Else
                    sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & prefil & ") "
                End If
                sql += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Products.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND ProductItems.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Products.FormP01Flag = 'Y' "
                sql += " ORDER BY BudgetRevPlans.FundCenter_FundCenterCode, Products.ProductCode, ProductItems.ItemNo"

                adapter.GetDataByFilter(datatable, sql)
                'Response.Write(sql)

            End If

            'Response.Write(sql)

            datatable.Columns("FundCenter_FundCenterCode").AllowDBNull = True
            'datatable.Columns("FundCenter_PlanYear").AllowDBNull = True

            'datatable.Columns("Fund_FundCode").AllowDBNull = True
            'datatable.Columns("Fund_PlanYear").AllowDBNull = True

            datatable.Columns("Id").ReadOnly = False
            Dim idx As Integer = 1
            For Each row As DataRow In datatable
                If idx = 6 Then
                    For j = 6 To 34
                        row(j) = row(j) * 100
                    Next
                    idx = 0
                End If
                idx += 1
            Next
            'Response.Write(Val(Session("FCTR")))
            e.DataSource = datatable


            'datatable = adapter.GetDataByMultiple(Val(Session("PlanYear")), Val(Session("Versions")), Session("users"))
        Else
            Dim sql As String
            Dim fctr As String = ""
            fctr = Session("search_fctr")

            sql = " select BudgetRevPlans.id,BudgetRevPlans.FundCenter_FundCenterCode,FundCenters.Description As FCName,BudgetRevplans.Product_ProductCode As ProductCode,Products.Description As ProductDesc,BudgetRevPlans.ProductItem_Id,ProductItems.Description As ProItemDesc"
            sql += " ,ActualLastYear2,ActualLastYear1,ForecastLastYear1,ForecastCurYear,LastYearMonth1,LastYearMonth2,LastYearMonth3,LastYearMonth4,LastYearMonth5,LastYearMonth6,LastYearMonth7,LastYearMonth8,LastYearMonth9,LastYearMonth10,LastYearMonth11,LastYearMonth12,CurYearMonth1,CurYearMonth2,CurYearMonth3,CurYearMonth4,CurYearMonth5,CurYearMonth6,CurYearMonth7,CurYearMonth8,CurYearMonth9,CurYearMonth10,CurYearMonth11,CurYearMonth12,(ActualLastYear1+ForecastLastYear1) As SumFX"
            sql += " , ProductItems.ItemNo"
            sql += " From budgetRevPlans INNER JOIN FundCenters ON BudgetRevPlans.FundCenter_FundCenterCode = FundCenters.FundCenterCode"
            sql += " INNER JOIN Products ON BudgetRevPlans.Product_ProductCode = Products.ProductCode INNER JOIN ProductItems ON BudgetRevplans.ProductItem_Id = ProductItems.Id"
            sql += " And ProductItems.FormID = 'P01' "

            sql += " AND BudgetRevPlans.PlanYear = " & PlanYearsTxt.Text & " "
            sql += " AND BudgetRevPlans.Version = '" & VersionTxt.Text & "'"
            If fctr <> "" Then
                sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & fctr & ") "
            Else
                sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & prefil & ") "
            End If
            sql += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "
            sql += " AND Products.PlanYear = " & PlanYearsTxt.Text & " "
            sql += " AND ProductItems.PlanYear = " & PlanYearsTxt.Text & " "
            sql += " AND Products.FormP01Flag = 'Y' "
            sql += " ORDER BY BudgetRevPlans.FundCenter_FundCenterCode, Products.ProductCode, ProductItems.ItemNo"
            adapter.GetDataByFilter(datatable, sql)
            'Response.Write(sql)
            'datatable = adapter.GetDataByMultiple2(Val(Session("PlanYear")), Val(Session("Versions")), Session("users"))




            'datatable.Columns("FundCenter_PlanYear").AllowDBNull = True

            'datatable.Columns("Fund_FundCode").AllowDBNull = True
            'datatable.Columns("Fund_PlanYear").AllowDBNull = True

            datatable.Columns("Id").ReadOnly = False
            Dim idx As Integer = 1
            For Each row As DataRow In datatable
                If idx = 6 Then
                    For j = 6 To 34
                        row(j) = row(j) * 100
                    Next
                    idx = 0
                End If
                idx += 1
            Next
            'Response.Write(Val(Session("FCTR")))
            e.DataSource = datatable
        End If

        'WebGrid1.RootTable.ColumnSets.GetNamedItem("ColumnSet01").Caption = "ผลจริงปี " & Val(PlanYearsTxt.Text) - 1
        'WebGrid1.RootTable.ColumnSets.GetNamedItem("ColumnSet02").Caption = "ประมาณการปี " & Val(PlanYearsTxt.Text) - 1
        'WebGrid1.RootTable.ColumnSets.GetNamedItem("ColumnSet03").Caption = "ประมาณการปี " & Val(PlanYearsTxt.Text)
    End Sub
    Dim old_fctr As String = ""
    Dim old_prod As String = ""
    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' Dim area_level As Decimal = Val(e.Row.Cells.GetNamedItem("area_level").Text)
        'If area_level = 9 Then
        'e.Row.Style.BackColor = Drawing.Color.Coral
        ' End If

        If e.Row.Type = RowType.Record Then
            If lockhash(e.Row.Cells(2).Value.ToString.Trim) = True Then
                e.Row.Cells(0).Column.IsRowChecker = False
                e.Row.ForceNoEdit = True
                e.Row.Style.BackColor = Drawing.Color.Gray
            Else
                e.Row.Cells(0).Column.IsRowChecker = True
            End If
        End If


        'e.Row.Cells(4).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
        e.Row.Cells(5).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
        e.Row.Cells(6).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
        e.Row.Cells(7).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
        e.Row.Cells(8).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
        e.Row.Cells(9).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
        e.Row.Cells(10).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
        e.Row.Cells(11).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
        'e.Row.Cells(12).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")


        e.Row.Cells(19).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
        e.Row.Cells(20).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
        'e.Row.Cells(22).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
        e.Row.Cells(33).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
        'e.Row.Cells(37).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")

        Dim dr As DataRowView = DirectCast(e.Row.DataRow, DataRowView)
        If e.Row.Type = RowType.Record Then
            e.Row.ForceNoEdit = True
            Dim code As String = dr("ItemNo").ToString
            If code = 3 Or code = 4 Or code = 5 Then

                e.Row.Cells(12).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(13).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(14).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(15).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(16).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(17).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(18).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(19).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(20).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(21).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(22).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(23).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(24).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(25).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(26).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(27).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(28).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(29).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(25).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(26).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(27).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(28).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(29).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(30).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(31).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(32).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")
                e.Row.Cells(33).Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#ABABAB")


            End If
            If Val(code) = 6 Then
                'Dim multiply As Integer = 1
                ''If WebGrid1.IsFlyPostBack = True Then
                ''    multiply = 1
                ''End If
                'For i = 4 To e.Row.Cells.Count - 1

                '    e.Row.Cells(i).Value = e.Row.Cells(i).Value * multiply


                'Next
                Dim sum1 As Decimal = (dr("LastYearMonth1") + dr("LastYearMonth2") + dr("LastYearMonth3") + dr("LastYearMonth4") + dr("LastYearMonth5"))
                Dim sum2 As Decimal = (dr("LastYearMonth6") + dr("LastYearMonth7") + dr("LastYearMonth8") + dr("LastYearMonth9") + dr("LastYearMonth10") + dr("LastYearMonth11") + dr("LastYearMonth12"))
                e.Row.Cells(11).Value = (sum1 / 5)
                e.Row.Cells(19).Value = (sum2 / 7)
                e.Row.Cells(20).Value = ((sum1 + sum2) / 12)

                Dim sum3 As Decimal = (dr("CurYearMonth1") + dr("CurYearMonth2") + dr("CurYearMonth3") + dr("CurYearMonth4") + dr("CurYearMonth5") + dr("CurYearMonth6") + dr("CurYearMonth7") + dr("CurYearMonth8") + dr("CurYearMonth9") + dr("CurYearMonth10") + dr("CurYearMonth11") + dr("CurYearMonth12"))
                e.Row.Cells(33).Value = (sum3 / 12)
            End If



            'Dim fctr As String = dr("FundCenter_FundCenterCode").ToString()
            'Dim prod As String = dr("ProductCode").ToString()
            'If fctr = old_fctr And prod = old_prod Then
            If code <> 1 Then
                e.Row.Cells(2).Text = ""
                e.Row.Cells(2).Value = ""
                e.Row.Cells(3).Text = ""
                e.Row.Cells(3).Value = ""
            End If
            'old_fctr = fctr
            'old_prod = prod


            If code = 6 Then
                e.Row.Style.BorderSettings.Bottom.Width = 3
                e.Row.Style.BorderSettings.Bottom.Style = BorderStyle.Solid
                e.Row.Style.BorderSettings.Bottom.Color = Drawing.Color.DarkGray
            End If
        End If


    End Sub

    Protected Sub WebGrid1_InitializeColumn(sender As Object, e As ColumnEventArgs) Handles WebGrid1.InitializeColumn
        If e.Column.Name = "result1" Then
            e.Column.Caption = "ผลจริงปี</br> " & Val(PlanYearsTxt.Text) - 2

        End If

        If e.Column.Name = "truey1" Then
            e.Column.Caption = "ผลจริง</br>ม.ค. " & (Val(PlanYearsTxt.Text) - 1).ToString

        End If

        If e.Column.Name = "truey2" Then
            e.Column.Caption = "ผลจริง</br>ก.พ. " & (Val(PlanYearsTxt.Text) - 1).ToString

        End If

        If e.Column.Name = "truey3" Then
            e.Column.Caption = "ผลจริง</br>มี.ค. " & (Val(PlanYearsTxt.Text) - 1).ToString

        End If
        If e.Column.Name = "truey4" Then
            e.Column.Caption = "ผลจริง</br>เม.ย. " & (Val(PlanYearsTxt.Text) - 1).ToString

        End If
        If e.Column.Name = "truey5" Then
            e.Column.Caption = "ผลจริง</br>พ.ค. " & (Val(PlanYearsTxt.Text) - 1).ToString

        End If
        If e.Column.Name = "tobtanx11" Then
            e.Column.Caption = "รวมผลจริง</br> " & (Val(PlanYearsTxt.Text) - 1).ToString

        End If
        If e.Column.Name = "truey6" Then
            e.Column.Caption = "ประมาณการ</br>มิ.ย. " & (Val(PlanYearsTxt.Text) - 1).ToString

        End If
        If e.Column.Name = "truey7" Then
            e.Column.Caption = "ประมาณการ</br>ก.ค. " & (Val(PlanYearsTxt.Text) - 1).ToString

        End If
        If e.Column.Name = "truey8" Then
            e.Column.Caption = "ประมาณการ</br>ส.ค. " & (Val(PlanYearsTxt.Text) - 1).ToString

        End If
        If e.Column.Name = "truey9" Then
            e.Column.Caption = "ประมาณการ</br>ก.ย. " & (Val(PlanYearsTxt.Text) - 1).ToString

        End If
        If e.Column.Name = "truey10" Then
            e.Column.Caption = "ประมาณการ</br>ต.ค. " & (Val(PlanYearsTxt.Text) - 1).ToString

        End If
        If e.Column.Name = "truey11" Then
            e.Column.Caption = "ประมาณการ</br>พ.ย. " & (Val(PlanYearsTxt.Text) - 1).ToString

        End If
        If e.Column.Name = "truey12" Then
            e.Column.Caption = "ประมาณการ</br>ธ.ค. " & (Val(PlanYearsTxt.Text) - 1).ToString

        End If
        If e.Column.Name = "trueyX1" Then
            e.Column.Caption = "รวมประมาณการ</br> " & (Val(PlanYearsTxt.Text) - 1).ToString

        End If
        If e.Column.Name = "trueyX2" Then
            e.Column.Caption = "รวมปี</br> " & (Val(PlanYearsTxt.Text) - 1).ToString

        End If
        If e.Column.Name = "truey121" Then
            e.Column.Caption = "ประมาณการ</br>ม.ค. " & (Val(PlanYearsTxt.Text)).ToString

        End If
        If e.Column.Name = "truey22" Then
            e.Column.Caption = "ประมาณการ</br>ก.พ. " & (Val(PlanYearsTxt.Text)).ToString

        End If
        If e.Column.Name = "truey32" Then
            e.Column.Caption = "ประมาณการ</br>มี.ค. " & (Val(PlanYearsTxt.Text)).ToString

        End If
        If e.Column.Name = "truey42" Then
            e.Column.Caption = "ประมาณการ</br>เม.ย. " & (Val(PlanYearsTxt.Text)).ToString

        End If
        If e.Column.Name = "truey52" Then
            e.Column.Caption = "ประมาณการ</br>พ.ค. " & (Val(PlanYearsTxt.Text)).ToString

        End If
        If e.Column.Name = "truey62" Then
            e.Column.Caption = "ประมาณการ</br>มิ.ย. " & (Val(PlanYearsTxt.Text)).ToString

        End If
        If e.Column.Name = "truey72" Then
            e.Column.Caption = "ประมาณการ</br>ก.ค. " & (Val(PlanYearsTxt.Text)).ToString

        End If
        If e.Column.Name = "truey82" Then
            e.Column.Caption = "ประมาณการ</br>ส.ค. " & (Val(PlanYearsTxt.Text)).ToString

        End If
        If e.Column.Name = "truey92" Then
            e.Column.Caption = "ประมาณการ</br>ก.ย. " & (Val(PlanYearsTxt.Text)).ToString

        End If
        If e.Column.Name = "truey102" Then
            e.Column.Caption = "ประมาณการ</br>ต.ค. " & (Val(PlanYearsTxt.Text)).ToString

        End If
        If e.Column.Name = "truey112" Then
            e.Column.Caption = "ประมาณการ</br>พ.ย. " & (Val(PlanYearsTxt.Text)).ToString

        End If
        If e.Column.Name = "truey122" Then
            e.Column.Caption = "ประมาณการ</br>ธ.ค. " & (Val(PlanYearsTxt.Text)).ToString

        End If
        If e.Column.Name = "trueyX23" Then
            e.Column.Caption = "รวมประมาณการ</br> " & (Val(PlanYearsTxt.Text)).ToString

        End If

        If e.Column.ColumnType = ColumnType.CheckBox And lockhash.Count > 0 Then
            e.Column.IsRowChecker = False
            e.Column.Width = 24
            e.Column.Caption = " "
        End If

    End Sub
    Protected Sub FundCenterCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles FundCenterCombo.InitializeDataSource
        Dim ds As New DataSet
        'If Session("Versions") > 1 Then
        If IsPostBack = True And Session("Search") = "true" Then
            Dim fctr As String = ""
            fctr = Session("search_fctr")
            Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
            Dim dt As New BasicData.FCTR_Child2DataTable
            Dim sql = ""
            sql += " WHERE 1 = 1 "
            sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
            If fctr <> "" Then
                sql += " AND fctr_view.FundCenterCode IN (" & fctr & ") "
            End If
            sql += "ORDER BY [Level] ASC, FundCenterCode ASC"
            adap.GetDataByFilter(dt, sql)
            e.DataSource = dt
            'Response.Write(sql & "<br />")
        Else
            'ds = PublicFunction.GetChild2(Session("users"))
            'ds.Tables(0).DefaultView.Sort = "FundCenterCode ASC"
            'e.DataSource = ds
            Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
            Dim dt As New BasicData.FCTR_Child2DataTable
            Dim sql = ""
            Dim prefil As String = ""
            prefil = Session("prefilter")
            sql += " WHERE 1 = 1 "
            sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
            If prefil <> "" Then
                sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
            End If
            sql += "ORDER BY [Level] ASC, FundCenterCode ASC"
            adap.GetDataByFilter(dt, sql)
            e.DataSource = dt
        End If


        'Else
        'Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        'Dim dt As New BasicData.FCTR_ChildDataTable
        'dt = adap.GetOwnData(Session("FCTR"))
        'e.DataSource = dt
        'End If
    End Sub

    Protected Sub ProductCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles ProductCombo.InitializeDataSource

        Dim adapter As New SplitBudgetDSTableAdapters.ProductsTableAdapter

        Dim data As New SplitBudgetDS.ProductsDataTable
        'data = adapter.GetDataByPlanYear(Val(Session("PlanYear")))

        data = adapter.GetDataByP01(PlanYearsTxt.Text)
        e.DataSource = data
    End Sub




    Protected Sub WebGrid1_AddRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)


    End Sub

    Protected Sub WebGrid1_UpdateRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)

        'Dim adapter2 As New BasicDataTableAdapters.CmmtDetailsTableAdapter
        'Dim cmmt_id As Decimal = adapter2.GetCmmtId(Val(e.Row.Cells(2).Value))

        'adapter.UpdateQuery(Val(e.Row.Cells(4).Value), Val(e.Row.Cells(5).Value), e.Row.Cells(7).Value, e.Row.Cells(8).Value, Date.Now, Session("users"), e.Row.Cells(1).Value, e.Row.Cells(3).Value, cmmtcode, e.Row.Cells(2).Value, id)

        ' adapter.UpdateOTProject(Val(e.Row.Cells(4).Value), Val(e.Row.Cells(5).Value), e.Row.Cells(6).Value, e.Row.Cells(7).Value, e.Row.Cells(8).Value, e.Row.Cells(9).Value, Date.Now, Session("users"), e.Row.Cells(1).Value, e.Row.Cells(3).Value, cmmtcode, e.Row.Cells(2).Value, id)


    End Sub

    Sub PlotNode()

        Dim MyNode As WebTreeViewNode
        MyNode = New WebTreeViewNode()

        Dim adapter As New FundCenterParentTableAdapters.fctr_viewTableAdapter
        Dim dt As New FundCenterParent.fctr_viewDataTable
        dt = adapter.GetDataByUser(Session("users"), Val(PlanYearsTxt.Text))

        Dim root_id As String

        MyNode.Text = "GSB"
        MyNode.Value = "0"
        root_id = "0"

        If dt.Count > 0 Then

            WebTreeView3.Nodes.Add(MyNode)

            Dim rootpath = ""
            Dim prefil As String = ""
            prefil = Session("prefilter")
            rootpath = FctrClass.GetRootPrefil(Session("users"), Val(Session("planyear")), prefil)
            Dim rootarr() As String = Split(rootpath, ",")
            WebTreeView3.Nodes.Add(MyNode)

            For i = 0 To rootarr.Length - 1
                PlotChild(rootarr(i), MyNode, 1)
            Next
        End If

    End Sub

    Sub PlotChild(ByVal gpath As String, ByRef cnode As WebTreeViewNode, ByVal level As Decimal)
        'Response.Write(gpath & "<br>")
        Dim Hadapter As New FundCenterParentTableAdapters.FctHTableAdapter
        Dim Hdatatable As New FundCenterParent.FctHDataTable
        Hdatatable = Hadapter.GetDataByGPath(gpath, Val(Session("planyear")))

        Dim MyNode As WebTreeViewNode
        MyNode = New WebTreeViewNode()
        For Each row As FundCenterParent.FctHRow In Hdatatable


            MyNode.Text = row.Description
            MyNode.Value = row.FundCenter_FundCenterCode

            cnode.Nodes.Add(MyNode)
        Next


        If Len(gpath) >= 10 Then
            Exit Sub
        End If

        Dim hadap2 As New FundCenterParentTableAdapters.UserLikeGpathTableAdapter
        Dim hdt2 As New FundCenterParent.UserLikeGpathDataTable
        Dim sql As String = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")

        Dim pathadd As String = "__"
        'For i = 1 To 4
        sql = ""
        Dim fullpath As String = gpath & pathadd
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += " AND grouppath like '" & fullpath & "' "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND Fcth.PlanYear = " & Val(Session("PlanYear"))
        hadap2.GetDataByFilter(hdt2, sql)

        For Each row As FundCenterParent.UserLikeGpathRow In hdt2
            If Len(row.grouppath) <= 10 Then
                PlotChild(row.grouppath, MyNode, level + 1)
            End If

        Next

    End Sub

    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        GenerateValues()

        If TabContainer1.ActiveTab.ID = TabPanel1.ID Then
            Session.Remove("search_fctr")
            If WebCombo1.Value <> "" And WebCombo2.Value <> "" Then
                Session.Add("search_fctr", FctrClass.GetFromToFundCenter(WebCombo1.Value, WebCombo2.Value, Session("users"), Val(PlanYearsTxt.Text)))
            ElseIf WebCombo1.Value <> "" And WebCombo2.Value = "" Then
                Session.Add("search_fctr", FctrClass.GetFromToFundCenter(WebCombo1.Value, WebCombo1.Value, Session("users"), Val(PlanYearsTxt.Text)))
                'Response.Write(FctrClass.GetFromToFundCenter(WebCombo1.Value, WebCombo1.Value, Session("users")))
            ElseIf WebCombo1.Value = "" And WebCombo2.Value <> "" Then
                Session.Add("search_fctr", FctrClass.GetFromToFundCenter(WebCombo2.Value, WebCombo2.Value, Session("users"), Val(PlanYearsTxt.Text)))
                'Response.Write(FctrClass.GetFromToFundCenter(WebCombo2.Value, WebCombo2.Value, Session("users")))
            Else
                Session.Add("search_fctr", Session("prefilter"))
            End If

        ElseIf TabContainer1.ActiveTab.ID = TabPanel2.ID Then
            Session.Remove("search_fctr")
            If WebCombo3.Value = "" Then
                Session.Add("search_fctr", Session("prefilter"))
            Else
                Session.Add("search_fctr", FctrClass.GetSubFundCenter(WebCombo3.Value, Session("users"), Val(PlanYearsTxt.Text), Session("prefilter")))
            End If

        End If
        Session.Remove("Search")
        Session.Add("Search", "true")
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()


    End Sub

    Sub GenerateValues()
        If hdf1.Value.Length = 0 Then
            Session("Search") = "false"
        End If
        Try
            Dim Numx As Array = hdf1.Value.Split(",")


            For i = 0 To Numx.Length - 1
                ms8.Items.FindByValue(Numx(i).ToString).Selected = True
            Next
        Catch ex As Exception
            logger.Error(ex.Message)
            logger.Error(ex.StackTrace)
        End Try

    End Sub


    'Protected Sub coppy_btn_Click(sender As Object, e As ImageClickEventArgs) Handles coppy_btn.Click

    '    Dim checkedRows As ArrayList = WebGrid1.RootTable.GetCheckedRows()

    '    Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter

    '    For Each keyValue As String In checkedRows

    '        adapter.CoppyRow(keyValue)

    '    Next keyValue

    '    WebGrid1.ClearCachedDataSource()
    '    WebGrid1.RebindDataSource()

    'End Sub

    'Protected Sub delete_btn_Click(sender As Object, e As ImageClickEventArgs) Handles delete_btn.Click

    '    Dim checkedRows As ArrayList = WebGrid1.RootTable.GetCheckedRows()

    '    Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter

    '    For Each keyValue As String In checkedRows

    '        adapter.DeleteRow(keyValue)

    '    Next keyValue



    '    WebGrid1.ClearCachedDataSource()
    '    WebGrid1.RebindDataSource()

    'End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        'Exit Sub
        If e.Action = PostBackAction.Custom Then
            Dim ActionType As String = TryCast(Request("type"), String)
            If ActionType = "1" Then
                Dim checkedRows As ArrayList = WebGrid1.RootTable.GetCheckedRows()

                Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter

                For Each keyValue As String In checkedRows

                    adapter.CoppyRow(keyValue)
                    WebGrid1.ClientAction.Refresh()
                Next keyValue
            ElseIf ActionType = "2" Then
                '  Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter
                ' Dim adapter2 As New fundCenterTableAdapters.AttachmentsTableAdapter

                'adapter2.DeleteAttach(keyValue)
                'Adapter.DeleteRow(keyValue)
                ' WebGrid1.ClientAction.Refresh()
                'Next keyValue
                For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                    Dim adapter As New PDataSetTableAdapters.BudgetRevPlans1TableAdapter
                    Dim data As New PDataSet.BudgetRevPlans1DataTable
                    data = adapter.GetDataByID(keyValue)
                    'For Each row As PDataSet.BudgetRevPlans1Row In data
                    'adapter.DeleteRows(row.FundCenter_FundCenterCode, row.Product_ProductCode, row.PlanYear, row.Version)
                    'Next
                    Dim adapter2 As New PDataSetTableAdapters.ProductItemsTableAdapter
                    Dim data2 As New PDataSet.ProductItemsDataTable
                    If data.Count > 0 Then
                        data2 = adapter2.GetIdGroup(data.Item(0).ProductItem_Id)
                        For Each row2 As PDataSet.ProductItemsRow In data2
                            adapter.DeleteRows2(data.Item(0).FundCenter_FundCenterCode, data.Item(0).Product_ProductCode, data.Item(0).PlanYear, data.Item(0).Version, row2.Id)

                            logger.Info("User " & Session("users") & " delete product " & data.Item(0).Product_ProductCode & " of fund center " & data.Item(0).FundCenter_FundCenterCode)
                        Next
                    End If
                Next

                WebGrid1.ClientAction.Refresh()
            ElseIf ActionType = "3" Then

                'Dim row1 As String = TryCast(Request("row1"), String)
                'Dim row2 As String = TryCast(Request("row2"), String)
                'Dim row3 As String = TryCast(Request("row3"), String)
                'Dim row4 As String = TryCast(Request("row4"), String)
                'Dim row5 As String = TryCast(Request("row5"), String)
                'Dim row6 As String = TryCast(Request("row6"), String)

                'Dim adapterX As New PDataSetTableAdapters.BudgetRevPlans1TableAdapter
                'Dim adapterX2 As New PDataSetTableAdapters.ProductRevsTableAdapter


                'For i = 0 To 6
                '    If i = 0 Then
                '        Dim data1 As Array = row1.Split(";")
                '        'MsgBox(row1)
                '        'MsgBox(data1(0).ToString)
                '        adapterX.UpdateQuery(Val(data1(1).ToString), Val(data1(7).ToString), Val(data1(15).ToString), Val(data1(29).ToString), Val(data1(2).ToString), Val(data1(3).ToString), Val(data1(4).ToString), Val(data1(5).ToString), Val(data1(6).ToString), Val(data1(8).ToString), Val(data1(9).ToString), Val(data1(10).ToString), Val(data1(11).ToString), Val(data1(12).ToString), Val(data1(13).ToString), Val(data1(14).ToString), Val(data1(17).ToString), Val(data1(18).ToString), Val(data1(19).ToString), Val(data1(20).ToString), Val(data1(21).ToString), Val(data1(22).ToString), Val(data1(23).ToString), Val(data1(24).ToString), Val(data1(25).ToString), Val(data1(26).ToString), Val(data1(27).ToString), Val(data1(28).ToString), Date.Now(), Session("users"), Val(data1(0).ToString))
                '    ElseIf i = 1 Then
                '        Dim data1 As Array = row2.Split(";")
                '        '  MsgBox(data1(0).ToString)
                '        'MsgBox(row2)
                '        adapterX.UpdateQuery(Val(data1(1).ToString), Val(data1(7).ToString), Val(data1(15).ToString), Val(data1(29).ToString), Val(data1(2).ToString), Val(data1(3).ToString), Val(data1(4).ToString), Val(data1(5).ToString), Val(data1(6).ToString), Val(data1(8).ToString), Val(data1(9).ToString), Val(data1(10).ToString), Val(data1(11).ToString), Val(data1(12).ToString), Val(data1(13).ToString), Val(data1(14).ToString), Val(data1(17).ToString), Val(data1(18).ToString), Val(data1(19).ToString), Val(data1(20).ToString), Val(data1(21).ToString), Val(data1(22).ToString), Val(data1(23).ToString), Val(data1(24).ToString), Val(data1(25).ToString), Val(data1(26).ToString), Val(data1(27).ToString), Val(data1(28).ToString), Date.Now(), Session("users"), Val(data1(0).ToString))
                '    ElseIf i = 2 Then
                '        Dim data1 As Array = row3.Split(";")
                '        ' MsgBox(data1(0).ToString)
                '        'MsgBox(row3)
                '        adapterX.UpdateQuery(Val(data1(1).ToString), Val(data1(7).ToString), Val(data1(15).ToString), Val(data1(29).ToString), Val(data1(2).ToString), Val(data1(3).ToString), Val(data1(4).ToString), Val(data1(5).ToString), Val(data1(6).ToString), Val(data1(8).ToString), Val(data1(9).ToString), Val(data1(10).ToString), Val(data1(11).ToString), Val(data1(12).ToString), Val(data1(13).ToString), Val(data1(14).ToString), Val(data1(17).ToString), Val(data1(18).ToString), Val(data1(19).ToString), Val(data1(20).ToString), Val(data1(21).ToString), Val(data1(22).ToString), Val(data1(23).ToString), Val(data1(24).ToString), Val(data1(25).ToString), Val(data1(26).ToString), Val(data1(27).ToString), Val(data1(28).ToString), Date.Now(), Session("users"), Val(data1(0).ToString))
                '    ElseIf i = 3 Then
                '        Dim data1 As Array = row4.Split(";")
                '        'MsgBox(row4)
                '        adapterX.UpdateQuery(Val(data1(1).ToString), Val(data1(7).ToString), Val(data1(15).ToString), Val(data1(29).ToString), Val(data1(2).ToString), Val(data1(3).ToString), Val(data1(4).ToString), Val(data1(5).ToString), Val(data1(6).ToString), Val(data1(8).ToString), Val(data1(9).ToString), Val(data1(10).ToString), Val(data1(11).ToString), Val(data1(12).ToString), Val(data1(13).ToString), Val(data1(14).ToString), Val(data1(17).ToString), Val(data1(18).ToString), Val(data1(19).ToString), Val(data1(20).ToString), Val(data1(21).ToString), Val(data1(22).ToString), Val(data1(23).ToString), Val(data1(24).ToString), Val(data1(25).ToString), Val(data1(26).ToString), Val(data1(27).ToString), Val(data1(28).ToString), Date.Now(), Session("users"), Val(data1(0).ToString))
                '    ElseIf i = 4 Then
                '        Dim data1 As Array = row5.Split(";")
                '        'MsgBox(row5)
                '        adapterX.UpdateQuery(Val(data1(1).ToString), Val(data1(7).ToString), Val(data1(15).ToString), Val(data1(29).ToString), Val(data1(2).ToString), Val(data1(3).ToString), Val(data1(4).ToString), Val(data1(5).ToString), Val(data1(6).ToString), Val(data1(8).ToString), Val(data1(9).ToString), Val(data1(10).ToString), Val(data1(11).ToString), Val(data1(12).ToString), Val(data1(13).ToString), Val(data1(14).ToString), Val(data1(17).ToString), Val(data1(18).ToString), Val(data1(19).ToString), Val(data1(20).ToString), Val(data1(21).ToString), Val(data1(22).ToString), Val(data1(23).ToString), Val(data1(24).ToString), Val(data1(25).ToString), Val(data1(26).ToString), Val(data1(27).ToString), Val(data1(28).ToString), Date.Now(), Session("users"), Val(data1(0).ToString))
                '    ElseIf i = 5 Then
                '        Dim data1 As Array = row6.Split(";")
                '        'MsgBox(row6)
                '        ' MsgBox(data1(0).ToString)
                '        'adapterX.UpdateQuery(Val(data1(1).ToString), Val(data1(7).ToString), Val(data1(15).ToString), Val(data1(29).ToString), Val(data1(2).ToString), Val(data1(3).ToString), Val(data1(4).ToString), Val(data1(5).ToString), Val(data1(6).ToString), Val(data1(8).ToString), Val(data1(9).ToString), Val(data1(10).ToString), Val(data1(11).ToString), Val(data1(12).ToString), Val(data1(13).ToString), Val(data1(14).ToString), Val(data1(17).ToString), Val(data1(18).ToString), Val(data1(19).ToString), Val(data1(20).ToString), Val(data1(21).ToString), Val(data1(22).ToString), Val(data1(23).ToString), Val(data1(24).ToString), Val(data1(25).ToString), Val(data1(26).ToString), Val(data1(27).ToString), Val(data1(28).ToString), Date.Now(), Session("users"), Val(data1(0).ToString))
                '        adapterX.UpdateQuery(Val(data1(1).ToString) / 100, Val(data1(7).ToString) / 100, Val(data1(15).ToString) / 100, Val(data1(29).ToString) / 100, Val(data1(2).ToString) / 100, Val(data1(3).ToString) / 100, Val(data1(4).ToString) / 100, Val(data1(5).ToString) / 100, Val(data1(6).ToString) / 100, Val(data1(8).ToString) / 100, Val(data1(9).ToString) / 100, Val(data1(10).ToString) / 100, Val(data1(11).ToString) / 100, Val(data1(12).ToString) / 100, Val(data1(13).ToString) / 100, Val(data1(14).ToString) / 100, Val(data1(17).ToString) / 100, Val(data1(18).ToString) / 100, Val(data1(19).ToString) / 100, Val(data1(20).ToString) / 100, Val(data1(21).ToString) / 100, Val(data1(22).ToString) / 100, Val(data1(23).ToString) / 100, Val(data1(24).ToString) / 100, Val(data1(25).ToString) / 100, Val(data1(26).ToString) / 100, Val(data1(27).ToString) / 100, Val(data1(28).ToString) / 100, Date.Now(), Session("users"), Val(data1(0).ToString))
                '    End If
                'Next

                'WebGrid1.ClientAction.SetStatus("Ready.")
            End If
            aggregate_sum()
        End If

    End Sub

    Protected Sub WebCombo1_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo1.InitializeDataSource
        'If Session("Versions") > 1 Then
        'ds = PublicFunction.GetChild2(Session("users"))
        'ds.Tables(0).DefaultView.Sort = "FundCenterCode ASC"
        'e.DataSource = ds
        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear")) & " "
        sql += "ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)
        dt.Rows.Add("", "ทุกรายการ", "1", "0")
        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt



        'Else
        'Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        'Dim dt As New BasicData.FCTR_ChildDataTable
        'dt = adap.GetOwnData(Session("FCTR"))
        'e.DataSource = dt
        'End If
    End Sub

    Protected Sub WebCombo2_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo2.InitializeDataSource
        'If Session("Versions") > 1 Then
        'ds = PublicFunction.GetChild2(Session("users"))
        'ds.Tables(0).DefaultView.Sort = "FundCenterCode ASC"
        'e.DataSource = ds
        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear")) & " "
        sql += "ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)
        dt.Rows.Add("", "ทุกรายการ", "1", "0")
        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt


        'Else
        'Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        'Dim dt As New BasicData.FCTR_ChildDataTable
        'dt = adap.GetOwnData(Session("FCTR"))
        'e.DataSource = dt
        'End If
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub

    Sub aggregate_sum()
        Exit Sub
        Dim adapter As New P01_DSTableAdapters.SumAct2TableAdapter
        Dim datatable As New P01_DS.SumAct2DataTable
        Dim act1, act2, forlast, forecur As Decimal

        Dim prefil As String = ""
        prefil = Session("prefilter")
        'If Session("Versions") > 1 Then
        If IsPostBack = True And Session("Search") = "true" Then
            Dim sql As String
            sql = "SELECT ISNULL(SUM(ActualLastYear1),0) AS sumact1 , ISNULL(SUM(ActualLastYear2),0) AS sumact2, ISNULL(SUM(ForecastLastYear1),0) AS sumforelast1, ISNULL(SUM(ForecastCurYear),0) AS sumforecur1"
            sql += " From budgetRevPlans INNER JOIN FundCenters ON BudgetRevPlans.FundCenter_FundCenterCode = FundCenters.FundCenterCode"
            sql += " INNER JOIN Products ON BudgetRevPlans.Product_ProductCode = Products.ProductCode INNER JOIN ProductItems ON BudgetRevplans.ProductItem_Id = ProductItems.Id"

            Dim fctr As String = ""
            fctr = Session("search_fctr")
            If TabContainer1.ActiveTab.ID = TabPanel1.ID Then
                sql += " WHERE 1 = 1 "
                'sql += " WHERE BudgetDetails.FundCenter_FundCenterCode BETWEEN " & Val(WebCombo1.Value) & " AND " & Val(WebCombo2.Value

                If hdf1.Value.ToString <> "" Then
                    sql += " AND BudgetRevPlans.Product_ProductCode IN ("
                    Dim Numx As Array = hdf1.Value.Split(",")
                    For i = 0 To Numx.Length - 1
                        If Val(Numx(i)) <> 0 Then
                            If i <> 0 Then
                                sql += ","
                            End If
                            sql += Numx(i).ToString
                        End If

                    Next
                    sql += ") "
                End If

                sql += " AND BudgetRevPlans.Product_ProductCode IS NOT NULL "
                sql += " And ProductItems.FormID = 'P01' "

                sql += " AND BudgetRevPlans.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND BudgetRevPlans.Version = '" & VersionTxt.Text & "'"
                If fctr <> "" Then
                    sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & fctr & ") "
                Else
                    sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & prefil & ") "
                End If
                sql += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Products.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND ProductItems.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Products.FormP01Flag = 'Y' "
                sql += " AND ProductItems.ItemNo <> 6 "
                adapter.GetSumAct2(datatable, sql)
                'Response.Write(sql)

            ElseIf TabContainer1.ActiveTab.ID = TabPanel2.ID Then
                sql += " WHERE 1 = 1 "
                'sql += " WHERE BudgetDetails.FundCenter_FundCenterCode IN (SELECT FundCenter_FundCenterCode FROM FundCenterHierarchies "
                'sql += " WHERE FundCenter_FundCenterCode = " & Val(WebCombo3.Value) & " OR ParentFundCenterHierarchy_Id = " & Val(WebCombo3.Value) & ") "
                If hdf1.Value.ToString <> "" Then
                    sql += " AND BudgetRevPlans.Product_Productcode IN ("
                    Dim Numx As Array = hdf1.Value.Split(",")
                    For i = 0 To Numx.Length - 1
                        If Val(Numx(i)) <> 0 Then
                            If i <> 0 Then
                                sql += ","
                            End If
                            sql += Numx(i).ToString
                        End If
                    Next
                    sql += ") "
                End If


                sql += " AND BudgetRevPlans.Product_ProductCode IS NOT NULL "
                sql += " And ProductItems.FormID = 'P01' "

                sql += " AND BudgetRevPlans.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND BudgetRevPlans.Version = '" & VersionTxt.Text & "'"
                If fctr <> "" Then
                    sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & fctr & ") "
                Else
                    sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & prefil & ") "
                End If
                sql += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Products.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND ProductItems.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Products.FormP01Flag = 'Y' "
                sql += " AND ProductItems.ItemNo <> 6 "
                adapter.GetSumAct2(datatable, sql)
                'Response.Write(sql)

            End If

            'Response.Write(sql)



            'datatable = adapter.GetDataByMultiple(Val(Session("PlanYear")), Val(Session("Versions")), Session("users"))
        Else
            Dim sql As String
            Dim fctr As String = ""
            fctr = Session("search_fctr")

            sql = "SELECT ISNULL(SUM(ActualLastYear1),0) AS sumact1 , ISNULL(SUM(ActualLastYear2),0) AS sumact2, ISNULL(SUM(ForecastLastYear1),0) AS sumforelast1, ISNULL(SUM(ForecastCurYear),0) AS sumforecur1"
            sql += " From budgetRevPlans INNER JOIN FundCenters ON BudgetRevPlans.FundCenter_FundCenterCode = FundCenters.FundCenterCode"
            sql += " INNER JOIN Products ON BudgetRevPlans.Product_ProductCode = Products.ProductCode INNER JOIN ProductItems ON BudgetRevplans.ProductItem_Id = ProductItems.Id"
            sql += " And ProductItems.FormID = 'P01' "

            sql += " AND BudgetRevPlans.PlanYear = " & PlanYearsTxt.Text & " "
            sql += " AND BudgetRevPlans.Version = '" & VersionTxt.Text & "'"
            If fctr <> "" Then
                sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & fctr & ") "
            Else
                sql += " AND BudgetRevPlans.FundCenter_FundCenterCode IN (" & prefil & ") "
            End If
            sql += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "
            sql += " AND Products.PlanYear = " & PlanYearsTxt.Text & " "
            sql += " AND ProductItems.PlanYear = " & PlanYearsTxt.Text & " "
            sql += " AND Products.FormP01Flag = 'Y' "
            sql += " AND ProductItems.ItemNo <> 6 "
            adapter.GetSumAct2(datatable, sql)
            'MsgBox(sql)

        End If
        If datatable.Count > 0 Then
            act1 = datatable.Item(0).sumact1
            act2 = datatable.Item(0).sumact2
            forlast = datatable.Item(0).sumforelast1
            forecur = datatable.Item(0).sumforecur1
        End If
        'act1 = 11
        'act2 = 22
        'forlast = 33
        'forecur = 44

        WebGrid1.ClientAction.InvokeScript("SetFooter(6" & "," & act2 & ")")
        WebGrid1.ClientAction.InvokeScript("SetFooter(12" & "," & act1 & ")")
        WebGrid1.ClientAction.InvokeScript("SetFooter(21" & "," & forlast & ")")
        WebGrid1.ClientAction.InvokeScript("SetFooter(34" & "," & forecur & ")")
    End Sub

    Protected Sub WebCombo3_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo3.InitializeDataSource

        Dim adap As New FundCenterParentTableAdapters.SelectorTableAdapter
        Dim dt As New FundCenterParent.SelectorDataTable
        Dim dt2 As New FundCenterParent.SelectorDataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FctH.PlanYear = " & Val(Session("PlanYear"))
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += "ORDER BY Grouppath"
        adap.GetDataByFilter(dt2, sql)
        dt.Rows.Add("ทุกรายการ", "0", 0, "0", Val(Session("PlanYear")), "ทุกรายการ")
        dt.Merge(dt2)
        'dt.DefaultView.Sort = "FundCenter_FundCenterCode"
        e.DataSource = dt



    End Sub
End Class
